package com.dayaokeji.rhythmschoolstudent.client.common;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import com.dayaokeji.rhythmschoolstudent.client.common.base.App;
import com.dayaokeji.rhythmschoolstudent.client.mine.account.StudentIdLoginActivity;

import java.lang.ref.WeakReference;
import java.util.Stack;

/**
 * Created by Gorden on 2018/4/18.
 */

public class ActivityStackManager {

    private static Stack<Activity> stack;

    static {
        stack = new Stack<>();
    }


    public static void add(Activity activity) {
        if (activity != null) {
            stack.push(activity);
        }
    }


    public static void finishAll() {
        for (Activity activity : stack) {
            if (activity != null) {
                activity.finish();
            }
        }
        stack.clear();
    }

    public static void finish() {
        Activity activity = stack.lastElement();
        if (activity != null) {
            activity.finish();
        }
    }

    public static void finish(Activity activity) {
        if (activity != null) {
            activity.finish();
            stack.remove(activity);
        }
    }


    public static void appExit() {
        try{
            finishAll();
            @SuppressLint("ServiceCast")
            final ActivityManager activityManager = (ActivityManager) App.getInstance().getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
            activityManager.killBackgroundProcesses(App.getInstance().getPackageName());
            System.exit(0);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
